<?php

/*
 * This file will perform the ajax for checking a department has no ongoing route.
 * This is being used by admDepartmentSetup.php.
 * 
 * Created by: Peter Agno Jr.
 * Date created: November 29, 2011
 * 
 * In parameters: deptId
 * Out parameters: flag about the if department has no ongoing route
 */

//********************************************************************************************************
/*
 * Define the functions in here
 */
    
    // Start - Check if department has no ongoing route
    function noOngoingRoute($deptId) {
        $query = 
            "
                SELECT *
                FROM

                    -- You now have departments that have ongoing route
                    (SELECT DP.departmentId as departmentId
                    FROM RouteStep as RS, RouteStep_Dept_Pos as RSDP, Department_Position as DP,

                        -- You now have filtered the ongoing routes
                        (SELECT RS.routeId as routeId
                        FROM RouteStep as RS, 

                            -- Filter routesteps
                            (SELECT stepId
                            FROM Log
                            GROUP BY stepId) as OngoingSteps

                        WHERE OngoingSteps.stepId = RS.stepId
                        GROUP BY RS.routeId) as OngoingRoutes

                    WHERE OngoingRoutes.routeId = RS.routeId AND RS.stepId = RSDP.stepId AND RSDP.deptPosId = DP.deptPosId
                    GROUP BY DP.departmentId) as OngoingDepts

                WHERE departmentId = '$deptId'
            ";
        
        $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
        
        if ( mysql_fetch_array($result) == 0 ) {
            return true;
        }
        else {
            return false;
        }
    } // End - Check if department has an ongoing route
    
//********************************************************************************************************

session_start();

// Start - Checker for those users who will just go to the page by typing directly in the url.
if ($_POST) {
    include('../includes/siteConfig.php');
    
    // connect to database
    $connect = mysql_connect($hostName,$rootName,$dBasePassword) or die ('Unable to connect!');
    mysql_select_db($dBaseName) or die ('Unable to select database!');
    
    // Get the data from post
    $deptId = $_POST['deptId'];
    
    if ( noOngoingRoute($deptId) == true ) {
        $jsondata = array();
        $jsondata['flag'] = "itIsTrue";
        $feed[] = $jsondata;
        echo json_encode($feed);
    }
    else {
        $jsondata = array();
        $jsondata['flag'] = "itIsFalse";
        $feed[] = $jsondata;
        echo json_encode($feed);
    }
    
    mysql_close($connect);
}   // End - Checker for those users who will just go to the page by typing directly in the url.
else {
    echo "You are not authorized to view this page. This incident will be reported immediately.";
}
?>
